package cn.shyouchi.wuguanjia.android.ui.event.checknum;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.impl.ModuleItemClickListener;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class CheckNumManageActivity extends BaseActivity implements
        ModuleItemClickListener<CheckNumLineEntity.ResultMessageBean>
{


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_check_num_des)
    RecyclerView mRvCheckNumDes;
    @BindView(R.id.btn_add_check_num_des)
    Button mBtnAddCheckNumDes;
    EventApi mEventApi;
    String projectId;

    CheckNumLineAdapter mLineAdapter;
    List<CheckNumLineEntity.ResultMessageBean> mNumLineEntities = new ArrayList<>();

    boolean needRefresh = true;

    @Override
    public int initContentView() {
        return R.layout.activity_check_num_manage;
    }

    @Override
    public void initInjector() {
        projectId = getIntent().getStringExtra("projectId");
        mEventApi = new EventApi(mContext);
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());


        EventBus.getDefault().register(this);


        mLineAdapter = new CheckNumLineAdapter(mNumLineEntities, this);
        mRvCheckNumDes.setLayoutManager(new LinearLayoutManager(mContext));
        mRvCheckNumDes.setAdapter(mLineAdapter);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.btn_add_check_num_des)
    public void onClick() {
        Intent intent = new Intent(mContext, AddCheckNumDesActivity.class);
        intent.putExtra("projectid", projectId);
        startActivity(intent);
    }

    public void fetchLines() {
        mEventApi.getCheckNumList(projectId)
                .subscribeOn(Schedulers.io())
                .map(new Func1<CheckNumLineEntity, List<CheckNumLineEntity.ResultMessageBean>>() {
                    @Override
                    public List<CheckNumLineEntity.ResultMessageBean> call(CheckNumLineEntity entity) {
                        return entity.getResultMessage();
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<List<CheckNumLineEntity.ResultMessageBean>>(mContext) {
                    @Override
                    public void onNext(List<CheckNumLineEntity.ResultMessageBean> resultMessageBeen) {
                        mNumLineEntities.clear();
                        mNumLineEntities.addAll(resultMessageBeen);
                        mLineAdapter.notifyDataSetChanged();
                    }
                });
    }


    @Override
    public void moduleItemClick(CheckNumLineEntity.ResultMessageBean model) {
        Intent intent = new Intent(mContext, EventCheckNumActivity.class);
        intent.putExtra("projectId", projectId);
        intent.putExtra("lineId", String.valueOf(model.getId()));
//        LogUtil.json(model);
        startActivity(intent);
    }

    @Override
    public void moduleItemLongClick(CheckNumLineEntity.ResultMessageBean model) {
        Intent intent = new Intent(mContext, AddCheckNumDesActivity.class);
        intent.putExtra("projectid", projectId);
        intent.putExtra("line",model);
        startActivity(intent);
    }

    @Override
    public void onDel(CheckNumLineEntity.ResultMessageBean model) {
        mEventApi.delCheckNumLine(String.valueOf(model.getId()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult result) {
                        fetchLines();
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
          fetchLines();
            needRefresh = false;
        }
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }
}
